/*
 * Created on 2005-8-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.jedi;

import java.util.ArrayList;
import java.util.List;

import com.tss.util.DbConn;
import com.tss.util.DbRs;
/**
 * @author dai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UserMgr extends BaseObj {
	public UserMgr() {}
	public User get(String userId,boolean isGetPower) {
		clearErr();
		User user = new User(userId);
		DbConn conn = new DbConn();
		String role_name = "";
		String role_desc = "";
		try {
			String sql = "select a.*,b.role_name,b.role_desc,b.show_order " +
				"from userinfo a inner join roleinfo b on a.role_id = b.role_id " +
				"where a.user_id = ?";
			conn.prepare(sql);
			conn.setString(1,userId);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			String role_id = get(rs,0,"role_id");
			user.setUserName(get(rs,0,"user_name"));
			user.setPwd(get(rs,0,"pwd"));
			user.setEmail(get(rs,0,"email"));
			user.setPhone(get(rs,0,"phone"));
			user.setMobile(get(rs,0,"mobile"));
			user.setCity(get(rs,0,"city"));
			user.setHeadImg(get(rs,0,"head_img"));
			user.setRoleId(role_id);
			user.setArticleNum(getInt(rs,0,"article_num"));
			role_name = get(rs,0,"role_name");
			role_desc = get(rs,0,"role_desc");
			
		
		List listUroles = new ArrayList();
		 sql = "select * from userrole where user_id = ?";

		conn.prepare(sql);
		conn.setString(1, userId);
		rs = conn.executeQuery();
		if (rs != null && rs.size() > 0) {
			for (int i = 0; i < rs.size(); i++) {
				Userrole uRole = new Userrole();
				uRole.setUser_id(get(rs, i, "user_id"));
				uRole.setRole_id(get(rs, i, "role_id"));

			

				listUroles.add(uRole);

			}
		}

		user.setRoles(listUroles);
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		Role role = new Role(user.getRoleId(),isGetPower);
		role.setRoleName(role_name);
		role.setRoleDesc(role_desc);
		user.setRole(role);
		return user;
	}
	
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "select a.*,b.role_name,b.role_desc,b.show_order " +
			"from userinfo a inner join roleinfo b on a.role_id = b.role_id " +
			
			"where 1=1 ";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				String role_id = get(rs,i,"role_id");
				User user = new User(get(rs,i,"user_id"));
				user.setUserName(get(rs,i,"user_name"));
				user.setPwd(get(rs,i,"pwd"));
				user.setEmail(get(rs,i,"email"));
				user.setPhone(get(rs,i,"phone"));
				user.setMobile(get(rs,i,"mobile"));
				user.setCity(get(rs,i,"city"));
				user.setHeadImg(get(rs,i,"head_img"));
				
				user.setRoleId(role_id);
				user.setArticleNum(getInt(rs,i,"article_num"));
				Role role = new Role(role_id,false);
				role.setRoleName(get(rs,i,"role_name"));
				role.setRoleDesc(get(rs,i,"role_desc"));
				user.setRole(role);
				
				
				DbConn connrole = new DbConn();
				DbRs rsrole = null;
				try
				{
				String sqlrole  = "select r.role_id, role_name, ur.user_id, r.show_order from userrole ur inner join roleinfo r on ur.role_id=r.role_id" +
						" where user_id=? order by show_order";
				
				connrole.prepare(sqlrole);
				connrole.setString(1, user.getId());							
				rsrole = connrole.executeQuery();
				}
				catch(Exception ex) {
					ex.printStackTrace();
					setErr(ex.getMessage());
					return null;
				}
				finally {
					connrole.close();
				}
				List roleList = new ArrayList();
				for (int j=0; j<rsrole.size(); j++){
					
					Role R = new Role(get(rsrole,j,"role_id"), false);
					R.setRoleName(get(rsrole, j, "role_name"));
					R.setShowOrder(getInt(rsrole, j, "show_order"));
					roleList.add(R);
						
				}

				user.setRoles(roleList);
				r.add(user);
												
			}
			return r;
		} 
		return null;
	}
	public List getUsersGroupbyroleid(String id) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "select  u.* from userinfo u" +
					" inner join userrole ur on u.user_id = ur.user_id " +
					" where 1=1 and ur.role_id='"+id.trim()+"'";
			
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				User user = new User(get(rs,i,"user_id"));
				user.setUserName(get(rs,i,"user_name"));
				user.setPwd(get(rs,i,"pwd"));
				user.setEmail(get(rs,i,"email"));
				user.setPhone(get(rs,i,"phone"));
				user.setMobile(get(rs,i,"mobile"));
				user.setRoleId(get(rs,i,"role_id"));
				user.setCity(get(rs,i,"city"));
				user.setHeadImg(get(rs,i,"head_img"));
			
			
				r.add(user);
			}
			return r;
		}
		return null;
	}
	public List getEngineers() {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "select  * from userinfo where 1=1 and role_id='engineer'";
			
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				
				User user = new User(get(rs,i,"user_id"));
				user.setUserName(get(rs,i,"user_name"));
				user.setPwd(get(rs,i,"pwd"));
				user.setEmail(get(rs,i,"email"));
				user.setPhone(get(rs,i,"phone"));
				user.setMobile(get(rs,i,"mobile"));
				user.setRoleId(get(rs,i,"role_id"));
				user.setCity(get(rs,i,"city"));
				user.setHeadImg(get(rs,i,"head_img"));
			
			
				r.add(user);
			}
			return r;
		}
		return null;
	}
	public User getUserById(String user_id) {
		clearErr();
		DbConn conn = new DbConn();
		User user = new User("user_id");
		DbRs rs = null;
		try {
			String sql = "select  * from userinfo where 1=1 and user_id=?";
			
			conn.prepare(sql);
			conn.setString(1,user_id);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				user.setUserName(get(rs,i,"user_name"));
				user.setPwd(get(rs,i,"pwd"));
				user.setEmail(get(rs,i,"email"));
				user.setPhone(get(rs,i,"phone"));
				user.setMobile(get(rs,i,"mobile"));
				user.setRoleId(get(rs,i,"role_id"));
				user.setCity(get(rs,i,"city"));
				user.setHeadImg(get(rs,i,"head_img"));
				r.add(user);
			}
			return user;
		}
		
		return null;
	}
}
